<?php

require_once './include/common.inc.php';
checkLogin();

$page_title = 'jans2en furniture - Products - Product Details';
$menu_id = 5;
getMyParam('pid');
getMyParam('gotype');
getMyParam('new');
getMyParam('curpage');
getMyParam('categoryid');
getMyParam('settingid');
getMyParam('collectionid');
getMyParam('styleid');

$new_url = '';
if ($new == 1) {
    $menu_id = 7;
    $new_url = '&new=1'; 
}

if ($fid==0 || $fid==$categoryid) {
    browseTracking(3,$pid,$categoryid);
} else {
    browseTracking(3,$pid,$fid . '||' . $categoryid);
}

//===========================check if the category is published=====//
$showProductCategory = true;  //the category of the product
if($fid >0 && $categoryid <=1000 )
{
	$sql = "select publish,(select publish from category where id = $fid) as f_publish from category where id = $categoryid";
	$publish_ary = $db->get_one($sql);
	if($publish_ary["publish"] == 0 || $publish_ary["f_publish"] == 0)
		$showProductCategory = false;
}
//=================================================================//


//еNext,Previewť--ʼ
if($gotype == "preview" || $gotype == "next")
{ 
	$pid_ary = array();
	$thisid = false;
	$new_pid = '';
	
	if($styleid >= 0 && $styleid !="")
	{
		$sql_style = " AND p.`style_id`= ".$styleid;		
		if($styleid == 0) $sql_style = "AND p.`style_id`>0";
		
		$sqlWhere = " WHERE c.publish=1 AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0 ".$sql_style." GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else if($settingid !=""  && $categoryid >= 0)
	{
		$cids = GetCategoryTreeIDS($_categories,$categoryid);
		if($settingid == 0 || $settingid == "") $strSettingSql = " AND p.`setting_id` > 0";
		else $strSettingSql = " AND p.`setting_id`= ".$settingid;
		
		$sqlWhere = " WHERE c.publish=1 AND c.`id` IN(".$cids.") AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0 ".$strSettingSql." GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else if($collectionid >= 0 && $collectionid!="")
	{
		$sql_collection = " AND p.`collection_id`= ".$collectionid;		
		if($collectionid == 0) $sql_collection = " AND p.`collection_id`>0";
		
		$sqlWhere = " WHERE c.publish=1 AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0 ".$sql_collection." GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else if($categoryid > 10000 && $settingid == "") //New Product
	{
		$sqlWhere = " WHERE c.publish=1 AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0  AND p.`new_flag`=1 GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY  p.`code` DESC ";
	}
	else if($categoryid >= 0 && $categoryid <=10000 && $settingid == "")
	{						
		$cids = GetCategoryTreeIDS($_categories,$categoryid);
		$sqlWhere = " WHERE c.publish=1 AND c.`id` IN(".$cids.") AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0  GROUP BY p.`id`";
		$sqlOrderBy = " ORDER BY p.`code` DESC ";
	}
	else
	{
		if($categoryid=="" ||$categoryid==0)
		{
			$sql = "SELECT c.* FROM `product_category` pc,`category` c WHERE c.id=pc.`category_id` AND pc.`product_id`='$pid' LIMIT 0,1";	
			$cate_info_ary = $db->get_one($sql);
			$categoryid = $cate_info_ary["id"];
			
			$cids = GetCategoryTreeIDS($_categories,$categoryid);
			$sqlWhere = " WHERE c.publish=1 AND c.`id` IN(".$cids.") AND c.`id`=pc.`category_id`  AND pc.`product_id`=p.`id`  AND p.`hidden_flag`=0  GROUP BY p.`id`";
			$sqlOrderBy = " ORDER BY p.`code` DESC ";
		}
	}
	
	/*if($categoryid > 10001)
	{
		$sql = "SELECT p.`id`, p.`code`, p.`name`, p.`pic`, p.`small_pic`, p.`price_1`, p.`price_2`, p.`price_3`, p.`new_flag` FROM `product` p, `product_category` pc, `category` c WHERE  c.`id`=pc.`category_id` AND pc.`product_id`=p.`id` AND p.`new_flag`=1 AND p.`hidden_flag`=0 and c.publish=1 GROUP BY p.`id` ORDER BY c.fid ASC, c.seq ASC, pc.seq desc, p.`code` DESC";		
	}
	else
	{ 
		$sql = "SELECT p.`id` FROM `product` p, `product_category` c, `category` ca WHERE  ca.`id` = c.`category_id` AND p.`id`=c.`product_id` AND  c.`category_id` IN(".$cids.") AND p.`hidden_flag`=0  and ca.publish=1 GROUP BY p.`id` ORDER BY ca.seq asc, c.seq desc, p.`code` DESC, p.`id` DESC";
	}*/
	$sql = "SELECT p.`id` FROM `product` p, `product_category` pc, `category` c".$sqlWhere.$sqlOrderBy;
	$query = $db->query($sql);
	$index_num = 0;
	while($rs = $db->fetch_array($query)) {
		if ($new_pid == '') {
			if ($gotype == "next" && $thisid) {
				$new_pid = $rs['id'];
			}
			if ($rs['id'] == $pid) {
				$thisid = true;
				if ($gotype == "preview") {
					$new_pid = empty($pid_ary) ? $pid : array_pop($pid_ary);
					$index_num--;
				}
			}
			$pid_ary[] = $rs['id'];
			$index_num++;
		}
	}
	if($new_pid != '')
	{
		$pid = $new_pid;	
	}
	$curpage = ceil($index_num/12);
}
//еNext,Previewť--


//===============================  ==================================//
$sql = "SELECT * FROM `product` WHERE `id`='$pid'";
$product_info_ary = $db->get_one($sql);

$clearance_flag = false;

$item_flag = '';
$product_info_ary['code'] = strtoupper($product_info_ary['code']);
if ($product_info_ary['new_flag'] == 1 && $clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>New! Clearance!</em></font>";
} elseif ($product_info_ary['new_flag'] == 1) {
    $item_flag = "<font color='red' size='2'><em>New!</em></font>";
} elseif ($clearance_flag) {
    $item_flag = "<font color='red' size='2'><em>Clearance!</em></font>";
}

if (trim($product_info_ary['pic']) == "" || !file_exists("zh/attachments/" . $product_info_ary['pic'])) {
    $product_info_ary['pic'] = 'no_big.gif';
}
$product_info_ary['name'] = ucwords(strtolower($product_info_ary['name']));
//$width_inch = sprintf("%0.2f", $product_info_ary['width']/2.54);
//$height_inch = sprintf("%0.2f", $product_info_ary['height']/2.54);
//$length_inch = sprintf("%0.2f", $product_info_ary['length']/2.54);

$product_info_ary['width'] = sprintf("%0.2f", $product_info_ary['width']);
$product_info_ary['height'] = sprintf("%0.2f", $product_info_ary['height']);
$product_info_ary['length'] = sprintf("%0.2f", $product_info_ary['length']);

$width_inch = sprintf("%0.2f", $product_info_ary['width2']);
$height_inch = sprintf("%0.2f", $product_info_ary['height2']);
$length_inch = sprintf("%0.2f", $product_info_ary['length2']);

$product_info_ary['cbm'] = sprintf("%0.2f", $product_info_ary['cbm']);
$product_info_ary['cuft'] = sprintf("%0.2f", $product_info_ary['cuft']);

$remarks = $product_info_ary['comment'] == '' ? '' : "<br>" . $product_info_ary['comment'];

if ($jansen_type==2) {
    $money_type = 'USD';
    $product_info_ary['price_2'] = number_format($product_info_ary['price_2'], 2, '.', ',');
    $price_show = $product_info_ary['price_2'];
} elseif ($jansen_type == 1) {
    $money_type = 'HKD';
    $product_info_ary['price_3'] = number_format($product_info_ary['price_3'], 2, '.', ',');
    $price_show = $product_info_ary['price_3'] ;
}
$product_info_ary['priceshown'] = number_format($product_info_ary['priceshown'], 2, '.', ',');

//================    ================//
$sql = "SELECT c.* FROM `product_category` pc,`category` c WHERE c.publish=1 AND c.id=pc.`category_id` AND pc.`product_id`='$pid' LIMIT 0,1";	
$cate_info_ary = $db->get_one($sql);
//if($categoryid=="" || $categoryid==0 || ($cate_info_ary["id"] > 0 && $categoryid != $cate_info_ary["id"]))
if($categoryid=="" || $categoryid==0 )
{
	$categoryid = $cate_info_ary["id"];
}
$vNull = null;
$listPage = 'product_photo_list.php';
if ($jansen_type > 0 && $jansen_uid > 0) 
{	
	$listPage = 'product_list.php';
}

$detailUrlParam = '';//URL,ԹRelated product洫

if($styleid >= 0 && $styleid!="")
{
	$listPage = $listPage.'?styleid=%d';
	$urlParam = 'styleid='.$styleid;
	if($styleid == 0) $styleid = $product_info_ary['style_id'];
	
	//$category_list =  GetCategoryTable($_style,$listPage,0,$styleid,0,&$vNull);	
	$category_list =  GetCategoryTableStyle($_style,$listPage,0,$styleid,0,&$vNull);	
	
	$postiondiv = GetPositionLink($_style,$styleid,"style.php","Style / Period","styleid",&$positionName);
}
else if($settingid != ""  && $categoryid >= 0  && $categoryid != "")
{
	$listPage = $listPage.'?categoryid=%d&settingid=%d';
	$urlParam = 'settingid='.$settingid.'&categoryid='.$categoryid;
	if($settingid == 0) $settingid = $product_info_ary['setting_id'];
	$detailUrlParam = 'settingid='.$settingid.'&categoryid='.$categoryid. '&';
	$arySetting = GetSettingAry();
	$category_list = GetSettingMenu($arySetting,$listPage,$categoryid,$settingid);
	
	$postiondiv = GetSettingPositionLink($arySetting,$listPage,$categoryid,$settingid,&$positionName);
}
else if($collectionid >= 0 && $collectionid !="")
{
	$listPage = $listPage.'?collectionid=%d';
	$urlParam = 'collectionid='.$collectionid;
	if($collectionid == 0) $collectionid = $product_info_ary['collection_id'];
	$category_list =  GetCategoryTable($_collection,$listPage,0,$collectionid,0,&$vNull);
	
	$postiondiv = GetPositionLink($_collection,$collectionid,"collections.php","Collection","collectionid",&$positionName);
}
else if($categoryid > 10000 && $settingid == "")//New Product
{
	$listPage = $listPage.'?categoryid=%d';
	$urlParam = 'categoryid='.$categoryid;
	
	$category_list = GetCategoryTable($_categories,$listPage,0,$categoryid,0,&$vNull);
	
    $postiondiv ='<div class="position" id="position"><a href="index.php">Home</a> &gt; <a href="products.php">Product</a> &gt; <a href="'.$listPage.'?categoryid=10001">New Product</a></div>';
}
else if($categoryid >= 0 && $categoryid <=10000 && $settingid == "" && $categoryid != "")	
{
	$listPage = $listPage.'?categoryid=%d';	
	$urlParam = 'categoryid='.$categoryid;
	
	$category_list = GetCategoryTable($_categories,$listPage,0,$categoryid,0,&$vNull);
	
	$postiondiv = GetProductCatePositionDiv($_categories,$categoryid,&$positionName);
}
else
{
	/*if($categoryid=="" ||$categoryid==0)
	{
		$sql = "SELECT c.* FROM `product_category` pc,`category` c WHERE c.id=pc.`category_id` AND pc.`product_id`='$pid' LIMIT 0,1";	
		$cate_info_ary = $db->get_one($sql);
		$categoryid = $cate_info_ary["id"];
		
		$listPage = $listPage.'?categoryid=%d';
		$urlParam = 'categoryid='.$categoryid;
		
		$category_list = GetCategoryTable($_categories,$listPage,0,$categoryid,0,&$vNull);
		
		$postiondiv = GetProductCatePositionDiv($_categories,$categoryid,&$positionName);
	}*/
}
//================   Ŀ¼Ϣ   ================//

//=============================== ƷϢ ==================================//

if($categoryid>0) {
	$strPage = $curpage != "" ? "&page=".$curpage : "";
    $return_href = "product_list.php?categoryid=".$categoryid.$strPage;
} 
else {
    $return_href = "javascript:history.back(1)";
}

$baner_img=GetBannerImg($positionName);

//Finishes array
$finish_item_ary = array();
$strDisplay ='style="display:none;"';
$strFirstFinishItem = '';
if($product_info_ary['finish_id'] > 0)
{
	$jsonFinishImagePath = 'FinishImagePath = {';
	$sql = 'select id,name,photo from p_finish_item where sid='.$product_info_ary['finish_id'].' order by orderindex';
	$query = $db->query($sql);
	$strSep = '';	
	$aryFinishItem = array();
	$i=0;
	while($rs=$db->fetch_array($query))
	{
		$finish_item_ary[$rs['id']] = $rs;
		
		$jsonFinishImagePath .= $strSep. '"Finish_'.$rs["id"].'":';
		if(trim($rs["photo"])!='' && file_exists("zh/attachments/".$rs["photo"]))
		{
			$rs["photo"] = 'zh/attachments/'.$rs["photo"];
		}
		else
		{
			$rs["photo"] = '';
		}
		$jsonFinishImagePath.= '"'.$rs["photo"].'"';
		$aryFinishItem[] = $rs;
		if($i==0)
		{
			$strFirstFinishItem = $rs["photo"];
		}
		$strSep = ',';			
		$i++;
	}
	$jsonFinishImagePath.='};';
	
	if($i > 0 && trim($strFirstFinishItem !='')) $strDisplay ='';
	else $strDisplay = " style='display:none'";
}


//Setting product
$strSep = '';
$jsonSettingImage = '';
$arySettingProduct = array();
if(is_numeric($pid)&&$pid>0)
{
	$hasset=array();
	$sql="select ps.`setting_id` From `product_p_setting` as ps join `p_setting` as p on ps.setting_id = p.id where ps.`product_id`='".$pid."' and ps.setting_id > 0";
	$query = $db->query($sql);
	while($rs=$db->fetch_array($query))
	{
		$hasset[]=$rs["setting_id"];		
	}	
	if(isset($hasset)&&count($hasset)>0)
	{
		$hasstr=implode(",",$hasset);			
		$hasset=array();
		$sql="select `product_id` From `product_p_setting` where `setting_id` in(".$hasstr.")";
		$query = $db->query($sql);		
		while($rs=$db->fetch_array($query))
		{
			$hasset[]=$rs["product_id"];		
		}
						
	    $k=array_search($pid,$hasset);	   
	    unset($hasset[$k]);	  	
		
		if(isset($hasset)&&count($hasset)>0)
		{
			$hasstr=implode(",",$hasset);
		
			$sql = 'select id,name,small_pic from product where `id` in('.$hasstr.') and hidden_flag=0 ';		
			$query = $db->query($sql);
			while($rs=$db->fetch_array($query))
			{			
				if($rs["small_pic"]=="" || !file_exists("zh/attachments/".$rs["small_pic"]))
				{
					$rs["small_pic"] = 'zh/attachments/no.gif';
				}
				else
				{
					$rs["small_pic"] = 'zh/attachments/'.$rs["small_pic"];
				}	
				$arySettingProduct[] = $rs;
			}
			$jsonSettingImage = "jsonSettingImage = ".json_encode($arySettingProduct);			
		}
	}
	
	//while($rs=$db->fetch_array($query))
}


// Wish list 
$sql = "SELECT `id`, `name`,`date` FROM `wishlist` WHERE `user_id` = $jansen_uid ORDER BY `date` DESC";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
	if($rs["name"] == '') $rs["name"] = "Created on ".date('d/M/Y \a\t H:i', $rs['date']);
	$wishlist_ary[] = $rs;
}


 $FabricImagePath="";
 $LeatherImagePath="";
 
//leather
$leathers_cat_ary=array();
$sql = "SELECT * FROM `p_leather_category` ORDER BY `seq`";
$query = $db->query($sql);
while($rsl = $db->fetch_array($query)) 
{
  $leathers_cat_ary[$rsl['id']]=$rsl['name'];		 
}

$LeatherImagePath='LeatherImagePath={';
$leathers_item_ary=array();
$LeatherImg=array();
$sql = "SELECT * FROM `p_leather` ORDER BY `seq`";
$query = $db->query($sql);
$rsl="";
while($rsl = $db->fetch_array($query)) 
{  
  // $leathers_item_ary[$rsl['cat_id']][$rsl['id']]=$rsl['name'];	
   $leathers_item_ary[$rsl['id']]=$rsl['name'];	
   if($rsl['photo']!=""&&file_exists("zh/attachments/".$rsl['photo']))
	 {
	   $LeatherImagePath.='"leatherimg_'.$rsl['id'].'":"zh/attachments/'.$rsl['photo'].'",';
	   $LeatherImg[$rsl['id']]='zh/attachments/'.$rsl['photo'];
	 }	 
}

if($LeatherImagePath=='LeatherImagePath={')
$LeatherImagePath="";
else
$LeatherImagePath.='"10000":"zh/attachments/no.jpg"}';	





//fabrics
$fabrics_cat_ary=array();
$sql = "SELECT * FROM `p_fabric_category` ORDER BY `seq`";
$query = $db->query($sql);

while($rsf = $db->fetch_array($query)) 
{		
   $fabrics_cat_ary[$rsf['id']]=$rsf['name'];
}

$FabricImagePath='FabricImagePath={';
$fabrics_item_ary=array();
$FabricImg=array();
$sql = "SELECT * FROM `p_fabric` ORDER BY `seq`";
$query = $db->query($sql);
$rsf="";
while($rsf = $db->fetch_array($query)) 
{		
  // $fabrics_item_ary[$rsf['cat_id']][]=array($rsf['id']=>$rsf['name']);
   	// $fabrics_item_ary[$rsf['cat_id']][$rsf['id']]=$rsf['name'];
	$fabrics_item_ary[$rsf['id']]=$rsf['name'];
	 $FabricImg[$rsf['id']]=$rsf['photo'];
	if($rsf['photo']!=""&&file_exists("zh/attachments/".$rsf['photo']))
	 {
	   $FabricImagePath.='"fabricimg_'.$rsf['id'].'":"zh/attachments/'.$rsf['photo'].'",';
	   $FabricImg[$rsf['id']]='zh/attachments/'.$rsf['photo'];
	 }	
	
	     
}
if($FabricImagePath=='FabricImagePath={')
$FabricImagePath="";
else
$FabricImagePath.='"10000":"zh/attachments/no.jpg"}';	

require_once PrintEot('header3');
require_once PrintEot('product_details');
require_once PrintEot('footer');

?>